﻿Imports BUS
Imports DTO
Imports System.ComponentModel
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Columns

Public Class ucPhieuChuyenKho
    Dim dsHangTonKho As List(Of TonKhoDTO) = TonKhoBUS.LayMangHangHoaTrongKho
    Dim dsCTChuyenKho As New BindingList(Of CTChuyenKhoDTO)

    Private Sub ucPhieuChuyenKho_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        btnLuuVaThem.Enabled = False

        Dim dsNhanVien As List(Of NhanVienDTO) = NhanVienBUS.LayMangNhanVien

        cbbMaNV.Properties.DataSource = dsNhanVien
        cbbMaNV.Properties.DisplayMember = "MaNhanVien"
        cbbMaNV.Properties.ValueMember = "MaNhanVien"

        cbbTenNVNhanHang.Properties.DataSource = dsNhanVien
        cbbTenNVNhanHang.Properties.DisplayMember = "TenNhanVien"
        cbbTenNVNhanHang.Properties.ValueMember = "MaNhanVien"

        dtpNgay.EditValue = DateTime.Now

        cbbNhanVienNhan.Properties.DataSource = dsNhanVien
        cbbNhanVienNhan.Properties.DisplayMember = "TenNhanVien"
        cbbNhanVienNhan.Properties.ValueMember = "MaNhanVien"

        cbbNhanVienNhan.EditValue = dsNhanVien(0).MaNhanVien

        TimMaPhieuChuyenKhoTuTang()

        'Gán datasource cho dgv

        dgvChuyenKho.DataSource = dsCTChuyenKho

        gridMaHang.DataSource = dsHangTonKho
        gridMaHang.DisplayMember = "MaHangHoa"
        gridMaHang.ValueMember = "MaHangHoa"
        gridMaHang.PopupFormSize = New Size(600, 400)

        gridHangHoa.DataSource = dsHangTonKho
        gridHangHoa.DisplayMember = "TenHangHoa"
        gridHangHoa.ValueMember = "MaHangHoa"
        gridHangHoa.PopupFormSize = New Size(600, 400)

        gridTuKho.DataSource = KhoHangBUS.LoadBangKhoHang
        gridTuKho.DisplayMember = "TenKhoHang"
        gridTuKho.ValueMember = "MaKhoHang"

        gridDenKho.DataSource = KhoHangBUS.LoadBangKhoHang
        gridDenKho.DisplayMember = "TenKhoHang"
        gridDenKho.ValueMember = "MaKhoHang"
    End Sub

    Sub TimMaPhieuChuyenKhoTuTang()
        'Tìm mã phiếu chuyển kho tự tăng
        Dim kq As String = CTChuyenKhoBUS.LayMaPhieuChuyenKhoTuTang("select max(Ma) from ChuyenKho").ToString()
        If kq = "" Then
            kq = "0"
        Else
            kq = kq
        End If

        txtPhieu.Text = "CK" + (Integer.Parse(kq) + 1).ToString().PadLeft(5, "0")
    End Sub

    Function TimNhanVienTheoMa(MaNV As String) As NhanVienDTO
        For Each nv As NhanVienDTO In NhanVienBUS.LayMangNhanVien
            If nv.MaNhanVien = MaNV Then
                Return nv
            End If
        Next
        Return Nothing
    End Function

    Private Sub cbbMaNV_EditValueChanged(sender As Object, e As EventArgs) Handles cbbMaNV.EditValueChanged
        Dim nv As NhanVienDTO = TimNhanVienTheoMa(cbbMaNV.EditValue.ToString)
        cbbTenNVNhanHang.EditValue = nv.MaNhanVien
    End Sub

    Private Sub cbbTenNVNhanHang_EditValueChanged(sender As Object, e As EventArgs) Handles cbbTenNVNhanHang.EditValueChanged
        Dim nv As NhanVienDTO = TimNhanVienTheoMa(cbbTenNVNhanHang.EditValue.ToString)
        cbbMaNV.EditValue = nv.MaNhanVien
    End Sub

    Function TimHangHoaTheoMa(MaHH As String) As TonKhoDTO
        For Each hangTonkho As TonKhoDTO In TonKhoBUS.LayMangHangHoaTrongKho
            If hangTonkho.MaHangHoa = MaHH Then
                Return hangTonkho
            End If
        Next
        Return Nothing
    End Function



    Private Sub GridViewChuyenKho_CellValueChanging(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles GridViewChuyenKho.CellValueChanging
        If e.Column.FieldName = clMaHang.FieldName Or e.Column.FieldName = clHangHoa.FieldName Then
            If GridViewChuyenKho.GetRowCellValue(e.RowHandle, clMaHang) Is Nothing _
                And GridViewChuyenKho.GetRowCellValue(e.RowHandle, clTuKho) Is Nothing _
                And GridViewChuyenKho.GetRowCellValue(e.RowHandle, clDenKho) Is Nothing _
                And GridViewChuyenKho.GetRowCellValue(e.RowHandle, clSoLuong) Is Nothing _
                And GridViewChuyenKho.GetRowCellValue(e.RowHandle, clDonGia) Is Nothing Then
                GridViewChuyenKho.AddNewRow()
            End If

            Dim hangTonKho As TonKhoDTO = dsHangTonKho.Find(Function(hh As TonKhoDTO)
                                                                Return hh.MaHangHoa = e.Value.ToString
                                                            End Function)
            GridViewChuyenKho.SetRowCellValue(e.RowHandle, clMaHang, e.Value)
            GridViewChuyenKho.SetRowCellValue(e.RowHandle, clHangHoa, e.Value)
            GridViewChuyenKho.SetRowCellValue(e.RowHandle, clDonVi, hangTonKho.DonVi)
            GridViewChuyenKho.SetRowCellValue(e.RowHandle, clTuKho, hangTonKho.MaKhoMacDinh)
            GridViewChuyenKho.SetRowCellValue(e.RowHandle, clDenKho, hangTonKho.MaKhoMacDinh)

        Else
            If e.Column.FieldName = clSoLuong.FieldName Or e.Column.FieldName = clDonGia.FieldName Then
                If GridViewChuyenKho.GetRowCellValue(e.RowHandle, clMaHang) Is Nothing _
                    And GridViewChuyenKho.GetRowCellValue(e.RowHandle, clHangHoa) Is Nothing _
                    And GridViewChuyenKho.GetRowCellValue(e.RowHandle, clTuKho) Is Nothing _
                    And GridViewChuyenKho.GetRowCellValue(e.RowHandle, clDenKho) Is Nothing _
                    And GridViewChuyenKho.GetRowCellValue(e.RowHandle, clSoLuong) Is Nothing _
                    And GridViewChuyenKho.GetRowCellValue(e.RowHandle, clThanhTien) Is Nothing _
                    And GridViewChuyenKho.GetRowCellValue(e.RowHandle, clDonGia) Is Nothing Then
                    GridViewChuyenKho.AddNewRow()
                End If
                'Nếu là column số lượng thì gán giá trị cho column số lượng
                If e.Column.FieldName = clSoLuong.FieldName Then
                    GridViewChuyenKho.SetRowCellValue(e.RowHandle, clSoLuong, e.Value)
                Else 'Ngược lại thì gán giá trị cho column đơn giá
                    GridViewChuyenKho.SetRowCellValue(e.RowHandle, clDonGia, e.Value)
                End If
                Dim soLuong As Double = Convert.ToDouble(GridViewChuyenKho.GetRowCellValue(e.RowHandle, clSoLuong).ToString)
                Dim donGia As Double = Convert.ToDouble(GridViewChuyenKho.GetRowCellValue(e.RowHandle, clDonGia).ToString)
                Dim thanhTien As Double = soLuong * donGia
                GridViewChuyenKho.SetRowCellValue(e.RowHandle, clThanhTien, thanhTien)
            End If
        End If

        btnLuuVaThem.Enabled = True
    End Sub


    Private Sub GridViewChuyenKho_CustomDrawRowIndicator(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs) Handles GridViewChuyenKho.CustomDrawRowIndicator
        If e.RowHandle >= 0 Then
            If e.Info.IsRowIndicator Then
                e.Info.DisplayText = (e.RowHandle + 1).ToString()
            End If
        End If
    End Sub

    Private Sub btnLuuVaThem_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnLuuVaThem.ItemClick
        If cbbMaNV.Text = "" Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Vui lòng chọn nhân viên", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If
        For i As Integer = 0 To dsCTChuyenKho.Count - 1
            If dsCTChuyenKho(i).MaHangHoa = "" Then
                Dim message As String
                message = String.Format("Chưa chọn mặt hàng." & vbNewLine & "Dòng số: {0}", i + 1)

                DevExpress.XtraEditors.XtraMessageBox.Show(message, "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                Return
            End If

            If dsCTChuyenKho(i).MaHangHoa = "" Then
                Dim message As String
                message = String.Format("Chưa chọn mặt hàng." & vbNewLine & "Dòng số: {0}", i + 1)
                DevExpress.XtraEditors.XtraMessageBox.Show(message, "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                Return
            End If

            If dsCTChuyenKho(i).TuKho = dsCTChuyenKho(i).DenKho Then
                Dim message As String = String.Format("Kho đến và kho đi trùng nhau." & vbNewLine & "Dòng số: {0}", i + 1)
                DevExpress.XtraEditors.XtraMessageBox.Show(message, "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                Return
            End If

            If dsCTChuyenKho(i).SoLuong <= 0 Then
                Dim message As String = String.Format("Số lượng không được <= 0." & vbNewLine & "Dòng số: {0}", i + 1)
                DevExpress.XtraEditors.XtraMessageBox.Show(message, "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                Return
            End If

            If dsCTChuyenKho(i).DonGia < 0 Then
                Dim message As String = String.Format("Đơn giá không được < 0." & vbNewLine & "Dòng số: {0}", i + 1)
                DevExpress.XtraEditors.XtraMessageBox.Show(message, "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                Return
            End If

            'mã hàng hóa hiện tại
            Dim maHangHienTai As String = dsCTChuyenKho(i).MaHangHoa

            'Số lượng hàng hóa hiện có trong kho
            Dim tempSL As String = CTChuyenKhoBUS.KiemTraSLHangTonTrongKho(dsCTChuyenKho(i)).ToString()
            If tempSL = "" Then
                tempSL = "0"
            End If
            If Convert.ToDouble(tempSL) < dsCTChuyenKho(i).SoLuong Then
                Dim hanghoaTemp As TonKhoDTO = dsHangTonKho.Find(Function(hh As TonKhoDTO)
                                                                     Return hh.MaHangHoa = maHangHienTai
                                                                 End Function)
                Dim message As String = String.Format("Mặt hàng {0} đã xuất quá số lượng tồn." & vbNewLine & _
                                                      "Số lượng tồn hiện tại là {1}" & vbNewLine & _
                                                      "Vui lòng nhập lại số lượng.", hanghoaTemp.TenHangHoa, tempSL)
                DevExpress.XtraEditors.XtraMessageBox.Show(message, "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                Return
            Else
                TonKhoBUS.CapNhatSLHangTonKhoDi(dsCTChuyenKho(i))

                If TonKhoBUS.CapNhatSLHangTonKhoDen(dsCTChuyenKho(i)) = 0 Then
                    TonKhoBUS.ThemHangTonKho(dsCTChuyenKho(i))
                End If
            End If
        Next

        Dim phieuCK As New ChuyenKhoDTO
        phieuCK.MaNhanVienGui = cbbMaNV.EditValue.ToString
        phieuCK.GhiChu = txtGhiChu.Text
        phieuCK.Ngay = DirectCast(dtpNgay.EditValue, Date)
        phieuCK.MaNhanVienNhan = cbbNhanVienNhan.EditValue.ToString
        phieuCK.ThanhTien = Convert.ToDouble(clThanhTien.SummaryItem.SummaryValue)
        Dim maPhieuCK As String = ChuyenKhoBUS.ThemPhieuChuyenKho(phieuCK)

        For i As Integer = 0 To dsCTChuyenKho.Count - 1
            dsCTChuyenKho(i).MaPhieuChuyenKho = maPhieuCK
            CTChuyenKhoBUS.ThemChiTietPhieuCK(dsCTChuyenKho(i))
        Next

        dsCTChuyenKho.Clear()
        TimMaPhieuChuyenKhoTuTang()
        txtGhiChu.EditValue = Nothing
    End Sub
End Class
